﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.Sticker
{
    /// <summary>CCPSIP_028：待接收禮物貼圖列表 API</summary>
    /// idHeader = 裝置 UUID 前八碼。
    /// checkcode = 裝置限時驗證碼。

    public class gift_list : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";


            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }


                //【02】取得待領取的GiftInfo Array
                //
                GiftInfo[] giftArray = OracleKitSticker.getGiftList(userInfo.SIP_Num);
                Dictionary<string, string>[] dictArray = new Dictionary<string, string>[giftArray.Length];

                for (int i = 0; i < dictArray.Length; i++)
                {
                    GiftInfo gInfo = giftArray[i];
                    Dictionary<string, string> newDict = new Dictionary<string, string>();

                    newDict["CreateTime"] = string.Format("{0:yyyy/MM/dd HH:mm:ss}", gInfo.CreateTime);
                    newDict["Sender"] = gInfo.Sender;
                    newDict["StickerID"] = gInfo.StickerID;

                    string senderName = SipKit.getCustomName(newDict["Sender"]);
                    string shortTime = string.Format("{0:M/d HH:mm}", gInfo.CreateTime);
                    if (gInfo.CreateTime.Year != DateTime.Now.Year)
                        shortTime = string.Format("{0:yyyy/M/d HH:mm}", gInfo.CreateTime);

                    newDict["Message"] = string.Format("{0} sent you a gift!\nWould you accept?"
                      + "\nSticker: {1}\nSent Time: {2}", senderName, newDict["StickerID"], shortTime);

                    dictArray[i] = newDict;
                }


                //【99】以JSON格式回傳
                //
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(dictArray);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
